import i18n from "../../i18n/config";
import { CHANGE_LANGUAGE } from "../language/languageActions";
//自定义中间件，异步修改i18n
export const languageMiddleware = (store) => (next) => (action) => {
  if (action.type === CHANGE_LANGUAGE) {
    // 使用Promise模拟异步操作
    return new Promise((resolve, reject) => {
      i18n
        .changeLanguage(action.payload)
        .then(() => {
          resolve(next(action)); // 成功后调用next将action传递给下一个中间件或者reducer
        })
        .catch((error) => {
          reject(error); // 如果有错误，抛出错误
        });
    });
  }

  return next(action); // 如果不是'CHANGE_LANGUAGE'类型的action，直接传递给下一个中间件或者reducer
};

// 
